package com.xinqi.modules.exam.student.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xinqi.common.mybatis.data.entity.BaseEntity;
import com.xinqi.modules.exam.student.dto.PaperAnswerDTO;
import com.xinqi.modules.exam.student.enums.PaperAnswerStatusEnum;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 课程测评学生答题实体层
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Setter
@Getter
@TableName("co_course_exam_paper_student")
public class CourseExamPaperStudentEntity extends BaseEntity {
    /**
     * 班级ID
     */
    @TableField(value = "class_id")
    private Long classId;
    /**
     * 测评ID
     */
    @TableField(value = "exam_paper_id")
    private Long examPaperId;
    /**
     * 学生ID
     */
    @TableField(value = "student_id")
    private Long studentId;
    /**
     * 试卷答案
     */
    @TableField(value = "paper_answer")
    private List<PaperAnswerDTO> paperAnswer;
    /**
     * 学生试卷状态，0-未交卷；1-未批阅；2-被打回；4-已批阅；
     */
    @TableField(value = "status")
    private PaperAnswerStatusEnum status;
    /**
     * 得分
     */
    @TableField(value = "score")
    private BigDecimal score;
    /**
     * 总分
     */
    @TableField(value = "total_score")
    private BigDecimal totalScore;
    /**
     * 题目答对数量
     */
    @TableField(value = "question_right_num")
    private Integer questionRightNum;
    /**
     * 题目总数
     */
    @TableField(value = "question_total")
    private Integer questionTotal;
    /**
     * 题型总数
     */
    @TableField(value = "question_type_total")
    private Integer questionTypeTotal;
    /**
     * 主观题数量
     */
    @TableField(value = "subjective_question_num")
    private Integer subjectiveQuestionNum;
    /**
     * 客观题数量
     */
    @TableField(value = "objective_question_num")
    private Integer objectiveQuestionNum;
    /**
     * 是否考试
     */
    @TableField(value = "is_exam")
    private Integer isExam;
    /**
     * 是否交卷
     */
    @TableField(value = "is_submit")
    private Integer isSubmit;
    /**
     * 是否强制交卷
     */
    @TableField(value = "is_force_submit")
    private Integer isForceSubmit;
    /**
     * 开始考试时间
     */
    @TableField(value = "start_time")
    private LocalDateTime startTime;
    /**
     * 最后开始考试时间
     */
    @TableField(value = "last_start_time")
    private LocalDateTime lastStartTime;
    /**
     * 提交次数
     */
    @TableField(value = "submit_num")
    private Integer submitNum;
    /**
     * 提交时间
     */
    @TableField(value = "submit_time")
    private LocalDateTime submitTime;
    /**
     * 是否限制答题时间
     */
    @TableField(value = "is_limit_answer_time")
    private Integer isLimitAnswerTime;
    /**
     * 考试时长（分钟）
     */
    @TableField(value = "exam_duration")
    private Long examDuration;
    /**
     * 剩余答题时间（分钟）
     */
    @TableField(value = "remaining_answer_time")
    private Long remainingAnswerTime;
    /**
     * 答题结束时间
     */
    @TableField(value = "answer_end_time")
    private LocalDateTime answerEndTime;
    /**
     * 是否已阅
     */
    @TableField(value = "is_marked")
    private Integer isMarked;
    /**
     * 是否系统已阅
     */
    @TableField(value = "is_sys_marked")
    private Integer isSysMarked;
    /**
     * 催交次数
     */
    @TableField(value = "call_num")
    private Integer callNum;
    /**
     * 最后一次催交时间
     */
    @TableField(value = "call_time")
    private LocalDateTime callTime;
    /**
     * 打回次数
     */
    @TableField(value = "repulse_num")
    private Integer repulseNum;
    /**
     * 切屏次数
     */
    @TableField(value = "screen_switch_num")
    private Integer screenSwitchNum;

    public PaperAnswerDTO findPaperAnswer(Long questionId) {
        if (CollectionUtils.isEmpty(this.paperAnswer)) {
            return null;
        }
        return this.paperAnswer.stream()
            .filter(paperAnswerDTO -> Objects.equals(paperAnswerDTO.getQuestionId(), questionId)).findFirst().orElse(null);
    }
}
